// 全局共享数据示例
import { BASE_H5_URL, SUB_URL, UPLOAD_URL, DEFAULT_NAME, DEFAULT_LOGO } from '@/constants'
import { useState, useEffect, useRef } from 'react'
import { getCapture, login, fetchUser, sendResetPwdSms, resetPwd, sendCode, loginByMobile } from '@/services/User'
import { getChannels } from '@/services/Order'
import { changeFavicon, getPlatTypeFromDomain, getAccessToken, getRoleList } from '@/utils/utils'
// import { createImportSpecifier } from 'typescript';

//  platformType 0 平台端   1 商家端

interface captureParams {
  phone: string
}

interface loginParams {
  username: string
  password: string
}

export default () => {
  const [name, setName] = useState<string>(DEFAULT_NAME!)
  const [logo, setLogo] = useState<string>(DEFAULT_LOGO!)
  // const [captrueRes, setCaptrueRes] = useState<any>();
  const [isLoading, setIsLoading] = useState<boolean>(false)
  const [routerList, setRouterList] = useState<any>()
  const [userInfo, setUserInfo] = useState<any>({})
  const [platformType, setPlatformType] = useState<number>(getPlatTypeFromDomain())
  const [roleList, setRoleList] = useState<any>(getRoleList())
  const [authList, setAuthList] = useState<any>([]) // 身份列表
  const [urlPrefix, setUrlPrefix] = useState<string>(SUB_URL![platformType])
  const [contentWidth, setContentWidth] = useState<number>(800)
  const [platformInfo, setPlatformInfo] = useState({})
  const [scopeSets, setScopeSets] = useState([])
  const [userAllInfo, setUserAllInfo] = useState<any>({}) // 用户所有信息

  const iframeRef = useRef(null)

  useEffect(() => {
    setUrlPrefix(SUB_URL![platformType])
  }, [platformType])

  // useEffect(() => {
  //   changeFavicon(logo)
  // }, [logo])

  const GetCapture = (data: captureParams): any => {
    return getCapture(data)
  }

  const Login = (data: loginParams) => {
    return login(data)
  }

  const SendResetPwdSms = (data: any): any => {
    return sendResetPwdSms(data)
  }

  const ResetPwd = (data: any): any => {
    return resetPwd(data)
  }

  const SendCode = (data: any): any => {
    return sendCode(data)
  }

  const LoginByMobile = (data: any): any => {
    return loginByMobile(data)
  }

  const CheckUser = () => {
    return new Promise((resolve, reject) => {
      const token = getAccessToken(platformType)
      if (token || (roleList && roleList.length)) {
        resolve({
          hasAccess: Boolean(token),
          hasRoleList: Boolean(roleList)
        })
      } else {
        reject(false)
      }
    })
  }

  const FetchUser = () => {
    return new Promise((resolve, reject) => {
      fetchUser().then(
        res => {
          resolve(res)
        },
        err => {
          reject(false)
        }
      )
    })
  }

  const GetRoleList = () => {
    const list = getRoleList()
    setRoleList(list)
    return list
  }

  // 检查操作权限
  const CheckOptionAuth = (key: string) => {
    return authList.includes(key)
  }

  // 获取当前渠道
  const GetChannels = async () => {
    let res = await getChannels()
    let arr = []
    if (res) {
      for (const resKey in res) {
        arr.push({
          label: resKey,
          // @ts-ignore
          name: res[resKey]
        })
      }
    }
    return arr
  }

  return {
    iframeRef,
    name,
    setName,
    logo,
    setLogo,
    isLoading,
    setIsLoading,
    routerList,
    setRouterList,
    userInfo,
    setUserInfo,
    platformType,
    setPlatformType,
    roleList,
    setRoleList,
    urlPrefix,
    setUrlPrefix,
    contentWidth,
    setContentWidth,
    authList,
    setAuthList,

    GetChannels,

    GetCapture,
    Login,
    SendResetPwdSms,
    ResetPwd,
    CheckUser,
    FetchUser,
    GetRoleList,
    UPLOAD_URL,
    BASE_H5_URL,
    CheckOptionAuth,

    setPlatformInfo,
    platformInfo,
    scopeSets,
    setScopeSets,

    SendCode,
    LoginByMobile,

    userAllInfo,
    setUserAllInfo
  }
}
